package com.day09;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Twinkle
 */
public class MyFrame extends JFrame {
    private static final int BUTTON_NUMBER = 6;
    private static final int PANEL_NUMBER = 3;
    private static final int TEXT_FIELD_NUMBER = 2;
    private JButton[] btn = new JButton[BUTTON_NUMBER];
    private JPanel[] panels = new JPanel[PANEL_NUMBER];
    private JTextField[] textFields = new JTextField[TEXT_FIELD_NUMBER];

    public MyFrame() {
        initComponent();
        addComponent();
        addListener();
        initWindows();
    }

    private void initComponent() {
//        btn[0] = new JButton("btn1");
//        btn[1] = new JButton("btn2");
//        btn[2] = new JButton("btn3");
//        btn[3] = new JButton("btn4");
//        btn[4] = new JButton("btn5");
//        btn[5] = new JButton("btn6");
        panels[0] = new JPanel();
        panels[1] = new JPanel();
        panels[2] = new JPanel();
        textFields[0] = new JTextField(20);
        textFields[1] = new JTextField(20);
        btn[0] = new JButton("加法");
        btn[0].setActionCommand("add");
        btn[1] = new JButton("减法");
        btn[1].setActionCommand("sub");
    }

    private void addComponent() {
        this.setLayout(new GridLayout(3, 1));
        panels[0].add(textFields[0]);
        panels[1].add(textFields[1]);
        panels[2].add(btn[0]);
        panels[2].add(btn[1]);
        this.add(panels[0]);
        this.add(panels[1]);
        this.add(panels[2]);

        //边界布局
        /*
        this.setLayout(new BorderLayout());
        this.add(btn[0], BorderLayout.CENTER);
        this.add(btn[1], BorderLayout.NORTH);
        this.add(btn[2], BorderLayout.SOUTH);
        this.add(btn[3], BorderLayout.WEST);
        this.add(btn[4], BorderLayout.EAST);
         */

        //网格布局
        /*
        this.setLayout(new GridLayout(3, 2));
        for (int i = 0; i < BUTTON_NUMBER; i++) {
            this.add(btn[i]);
        }
        */
        //流式布局
        /*
        this.setLayout(new FlowLayout());
         */

    }

    private class BtnListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton btn = (JButton) e.getSource();
            String name = btn.getActionCommand();
            if ("add".equals(name)) {
                String val = textFields[0].getText();
                int newVal = Integer.parseInt(val) + 1;
                textFields[1].setText(String.valueOf(newVal));
            } else if ("sub".equals(name)) {
                String val = textFields[0].getText();
                int newVal = Integer.parseInt(val) - 1;
                textFields[1].setText(String.valueOf(newVal));
            }
        }
    }

    private void addListener() {
        btn[0].addActionListener(new BtnListener());
        btn[1].addActionListener(new BtnListener());
    }

    private void initWindows() {
        this.setTitle("第2个窗体");
        this.setSize(300, 200);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        new MyFrame();
    }
}
